package com.key.org.service.serviceImp;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.Session;

import com.key.common.model.SelData;
import com.key.common.service.IDataListService;
import com.key.org.model.Post;
import com.key.util.HibernateUtil;

public class GetPostServiceImp implements IDataListService {

	@Override
	public List<SelData> getData(HttpServletRequest request, Object keyword) {
		// TODO Auto-generated method stub
		List<SelData> list = new ArrayList<SelData>();
		String keywords = null;
		if (keyword != null) {
			keywords = keyword.toString();
			request.setAttribute("keyword", keywords);
		}
		String departmentid = request.getParameter("departmentid");
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			session.beginTransaction();
			String hql = "from Post post";
			if (keywords != null) {
				hql += " where post.name like '%" + keywords + "%'";
				hql += " and post.department.id = '" + departmentid + "'";
			} else {
				hql += " where post.department.id = '" + departmentid + "'";
			}
			List<Post> postList = session.createQuery(hql).list();
			for (int i = 0; i < postList.size(); i++) {
				Post post = postList.get(i);
				SelData selData = new SelData();
				selData.setId(post.getId());
				selData.setDisplayName(post.getName());
				selData.setOtherInfo("");
				list.add(selData);
			}
			session.getTransaction().commit();
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		return list;
	}
}
